﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Structs;
using System.Xml;

namespace Analyzer
{
    class XMLparser
    {
        private static List<Word> words = new List<Word>();

        private static bool NotSign(string str)
        {
            return str != "," && str != ":" && str != ";" && str != "\""
                && str != "-" && str != "''" && str != "(" && str != ")";
        }

        private static void DelSign()
        {
            int i = 0;
            while ( i < words.Count-1)
            {
                if (words[i].Attributs[0] == "17"
                    || words[i].Attributs[0] == "11")
                {
                    words.Remove(words[i]);
                }
                else i++;
            }
        }

        //метод нахождения объекта в дереве по объекту
        private static Word FindWordInTree(List<Word> words, Word word)
        {
            Word result = null;
            for (int i = 0; i < words.Count; i++)
            {
                if (words[i] == word)
                {
                    return words[i];
                }
                    if (words[i].Words.Count != 0)
                    {
                        result = FindWordInTree(words[i].Words, word);
                    }
            }
            return result;
        }

        //тоже, только по слову
        private static Word FindWordInTree(List<Word> words, string word_form)
        {
            Word result = null;
            for (int i = 0; i < words.Count; i++)
            {
                if (words[i].Form == word_form)
                {
                    return words[i];
                }
                    if (words[i].Words.Count != 0)
                    {
                        result = FindWordInTree(words[i].Words, word_form);
                    }
            }
            return result;
        }

        //добавление узла в дереве
        private static void AddWord(Word left, Word right, string op)
        {
            switch (op)
            {
                case ">":
                    {
                        FindWordInTree(words, right).AddWord(left);
                        words.Remove(left);
                    }
                    break;
                case "<":
                    {
                        FindWordInTree(words, left).AddWord(right);
                        words.Remove(right);
                    }
                    break;
            }
        }
        
        //чтение xml
        public static List<Word> Read(string filepath)
        {
            words = new List<Word>();//обнуление дерева
            Word word;// = new Word(); //новый элемент списка слов

            string nodetype = string.Empty;//тип узла

            //связанные слова и оператор, определяющий что к чему
            Word leftword = new Word();
            Word rightword = new Word();
            string op = string.Empty;

            //тесты
            string test = string.Empty;

            //блок xml
            string block = string.Empty;

            XmlTextReader xmlreader = new XmlTextReader(filepath);
                while (xmlreader.Read())
                {
                    switch (xmlreader.NodeType)
                    {
                        case XmlNodeType.Element:
                            nodetype = xmlreader.Name;
                            switch (nodetype)
                            {
                                case "LinkList":
                                    {
                                        DelSign();
                                        block = nodetype;
                                    }
                                    break;
                                case "WordList":
                                    {
                                        block = nodetype;
                                    }
                                    break;
                                case "Word":
                                    {
                                        if (block == "WordList")
                                        {
                                            word = new Word();
                                            //word.Refresh();
                                            word.Norm = xmlreader.GetAttribute(0);
                                            word.Form = xmlreader.GetAttribute(1);
                                            words.Add(word);
                                        }
                                    }
                                    break;
                                case "Link":
                                    {
                                        op = xmlreader.GetAttribute(0);
                                        if (op == "<" || op == ">")
                                        {
                                            while (xmlreader.Name != "LeftWord")
                                            {
                                                xmlreader.Read();
                                            }
                                            test = xmlreader.Name;
                                            if (xmlreader.GetAttribute(2) != "11"
                                                && xmlreader.GetAttribute(2) != "17")
                                            {
                                                leftword = FindWordInTree(words, xmlreader.GetAttribute(1));
                                            }
                                            xmlreader.Read();
                                            while (xmlreader.Name != "RightWord")
                                            {
                                                xmlreader.Read();
                                            }
                                            if (xmlreader.GetAttribute(2) != "11"
                                                && xmlreader.GetAttribute(2) != "17")
                                            {
                                                rightword = FindWordInTree(words, xmlreader.GetAttribute(1));
                                                if (leftword != null && rightword != null)
                                                    AddWord(leftword, rightword, op);
                                            }
                                        }
                                    }
                                    break;
                            }
                            break;

                        case XmlNodeType.Text:
                            {
                                if (block == "WordList" && nodetype == "Attribute")
                                {
                                    {
                                        words[words.Count - 1].AddAttr(xmlreader.Value);
                                    }
                                }
                            }
                            break;
                    }
                }
            xmlreader.Close();
            return words;
        }
    }
}
